// =============================================
// DISCORD ROLE SETUP BOT
// =============================================
// SETUP STEPS:
// 1. Install Node.js from https://nodejs.org
// 2. Open a terminal in this folder and run:
//      npm init -y
//      npm install discord.js dotenv
// 3. Create a .env file in the same folder with:
//      TOKEN=MTQ2NjMwMzc1MDc0NTYyNDY1Ng.GGorFW.745NLAVS9O2XLy8cTuMXL_Rum7nTy8bQFbLH9Y
// 4. Run the bot with: node "discord bot.js"
// 5. In your Discord server type: !setuproles
//
// HOW TO GET A BOT TOKEN:
// - Go to https://discord.com/developers/applications
// - Click New Application, then go to Bot
// - Click Reset Token and copy it into .env
// - Under Bot, enable Message Content Intent
// - Go to OAuth2 > URL Generator
//   check: bot scope + Administrator permission
//   Open the URL to invite the bot to your server
// =============================================

require('dotenv').config();
const { Client, GatewayIntentBits, PermissionsBitField } = require('discord.js');

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent
  ]
});

const roles = [
  // --- IMAGE 1 ---
  { name: 'Fully Golden Peely',           color: 0xFFD700 }, // gold
  { name: 'OG Aerial Assault Trooper',    color: 0x8A8A8A }, // dark gray
  { name: 'OG Ghoul Trooper',             color: 0xA89DB8 }, // gray-purple
  { name: 'OG Renegade Raider',           color: 0x7B2D2D }, // dark brownish-red
  { name: 'OG Skull Trooper',             color: 0x9B59B6 }, // purple
  { name: 'Barricade',                    color: 0x9E9E9E }, // gray
  { name: 'Double Helix',                 color: 0xD0443C }, // orange-red
  { name: 'Honor Guard',                  color: 0x4A90D9 }, // blue
  { name: 'Wonder',                       color: 0x2ECC71 }, // green
  { name: 'Surf Strider',                 color: 0x1ABC9C }, // teal/cyan
  { name: 'Stealth Reflex',               color: 0xE8927C }, // salmon/coral
  { name: 'Dark Vertex',                  color: 0x8E44AD }, // purple
  { name: 'Royale Bomber',                color: 0xE67E22 }, // orange
  { name: 'Wildcat',                      color: 0xE8832A }, // orange
  { name: 'Clyde',                        color: 0xE8832A }, // orange
  { name: 'Eon',                          color: 0x27AE60 }, // green
  { name: 'Huntmaster Saber',             color: 0xD4AC0D }, // yellow-gold
  { name: 'Dark Skully',                  color: 0x7D6B8A }, // muted purple
  { name: 'Galaxy',                       color: 0x5B7DD8 }, // blue
  { name: 'Ikonik',                       color: 0xD0443C }, // orange-red
  { name: 'Black Knight',                 color: 0xCCCCCC }, // light gray
  { name: 'Glow',                         color: 0xBB8FCC }, // light purple
  { name: 'Rogue Spider Knight',          color: 0x2ECC71 }, // bright green
  { name: 'Thrilldiver',                  color: 0x4A90D9 }, // blue
  { name: 'Freediver',                    color: 0x9E9E9E }, // gray
  { name: 'Neo Versa',                    color: 0x9B59B6 }, // purple
  { name: 'Cobalt Snowfoot',              color: 0x1ABC9C }, // teal
  { name: 'Florin',                       color: 0x8E44AD }, // purple

  // --- IMAGE 2 ---
  { name: "What's Wrong, Toona Fish?",    color: 0xCCCCCC }, // white/light gray
  { name: 'Cameo Needs Loot',             color: 0x2ECC71 }, // green
  { name: 'Victory!',                     color: 0x9B59B6 }, // purple
  { name: 'Collegiate items',             color: 0x2ECC71 }, // green
  { name: 'StreamElements',               color: 0xF1C40F }, // gold/yellow
  { name: 'Llamas',                       color: 0xFF69B4 }, // hot pink
  { name: 'Repaz',                        color: 0xD4AC0D }, // gold/orange-yellow
  { name: 'lollek',                       color: 0xFFFFFF }, // white
  { name: 'Celebration Heart',            color: 0x9B59B6 }, // purple
  { name: 'Funflower',                    color: 0xD4C227 }, // yellow-green gold
  { name: 'Level 100',                    color: 0xBB44EE }, // bright purple/violet
  { name: 'Alpha Banner',                 color: 0x777777 }, // muted gray
  { name: 'YouTube Banner',               color: 0x777777 }, // muted gray

  // --- ADD-ONS ---
  { name: 'STW Founders',                 color: 0xE67E22 }, // orange
  { name: 'Ultimate STW Founders',        color: 0x888888 }, // muted gray
  { name: 'Rare Usernames',               color: 0xF1C40F }, // gold
  { name: 'Skin Collector',               color: 0x888888 }, // gray
  { name: 'PS+ Pack Collector',           color: 0x3498DB }, // blue
  { name: 'Spray Collector',              color: 0xCCCCCC }, // light gray
  { name: 'Rare Banners',                 color: 0x888888 }, // muted gray
  { name: 'Emote Collector',              color: 0xCCCCCC }, // light gray/white
];

client.once('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
  console.log('Send !setuproles in your server to create all roles.');
});

client.on('messageCreate', async (message) => {
  if (message.content !== '!setuproles') return;
  if (!message.member.permissions.has(PermissionsBitField.Flags.Administrator)) {
    return message.reply('You need Administrator permission to use this command.');
  }

  await message.reply(`Starting role creation... creating up to ${roles.length} roles. Please wait.`);

  let created = 0;
  let skipped = 0;
  const existingRoles = message.guild.roles.cache.map(r => r.name.toLowerCase());

  for (const role of roles) {
    if (existingRoles.includes(role.name.toLowerCase())) {
      console.log(`Skipped (already exists): ${role.name}`);
      skipped++;
      continue;
    }
    try {
      await message.guild.roles.create({
        name: role.name,
        color: role.color,
        reason: 'Bulk role setup via bot',
      });
      console.log(`Created: ${role.name}`);
      created++;
      await new Promise(res => setTimeout(res, 300));
    } catch (err) {
      console.error(`Failed to create "${role.name}": ${err.message}`);
    }
  }

  message.channel.send(`All done! Created **${created}** roles, skipped **${skipped}** that already existed.`);
});

client.login(process.env.TOKEN);
